//
//  EnterTheCategoryViewController.m
//  ShareYOU
//
//  Created by 杨亭 on 14-12-22.
//  Copyright (c) 2014年 ShareYOU. All rights reserved.
//

#import "EnterTheCategoryViewController.h"
#import "Payout.h"
#import "DataBaseHandler.h"


@interface EnterTheCategoryViewController ()

@end

@implementation EnterTheCategoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
       [super viewDidLoad];
    
    //设置背景图片
//    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"accountModule_background.png"]];
    
    self.view.backgroundColor = [UIColor whiteColor];

    CGFloat width = self.view.frame.size.width / 4;
    CGFloat height = self.view.frame.size.height / 6 ;

    //view显示
    //添加类型按钮
    self.addButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.addButton.frame = CGRectMake(width + 190, height * 2, 32, 32);
    self.addButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"add1.png"]];
    [self.addButton setTitle:@"" forState:UIControlStateNormal];
    [self.addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [self.addButton addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.addButton];

   
    //显示添加内容
    self.textFieldName = [[UILabel alloc] initWithFrame:CGRectMake(width - 10, height, 193, 65)];
    if ([self.str isEqualToString:@"addType"]) {
    self.textFieldName.text = @"添加类型内容:";

    }
    //判断添加成员
    else if ([self.str isEqualToString:@"addPeople"])
    {
       self.textFieldName.text = @"添加成员内容:";
    }

        self.textFieldName.font = [UIFont fontWithName:@"Arial" size:25];
//    self.textFieldName.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"showAccunt.png"]];
    [self.view addSubview:self.textFieldName];
    
    //输入添加内容
    self.textFieldStr = [[UITextField alloc] initWithFrame:CGRectMake(width - 20, height * 2, 200, 40)];
//    self.textFieldStr.backgroundColor = [UIColor cyanColor];
    //设置键盘为默认键盘,支持所有字符
    self.textFieldStr.keyboardType = UIKeyboardTypeDefault;
    //设置圆角边框，先给一个圆角半径的值
    self.textFieldStr.layer.cornerRadius = 10;
    self.textFieldStr.layer. masksToBounds = YES;
    //设置边框颜色
    self.textFieldStr.layer.borderWidth = 2;
    self.textFieldStr.layer.borderColor = [[UIColor colorWithRed:0.3 green:0.7 blue:1 alpha:1] CGColor];
    [self.view addSubview:self.textFieldStr];
 
    
    
    
    // Do any additional setup after loading the view.
}









////实现触摸空白回收键盘
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    UITextView *textView = (UITextView *)[self.view viewWithTag:100];
//    [textView resignFirstResponder];
//}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//添加按钮方法的实现
- (void)addButton:(id)sender
{
    if ([self.textFieldStr.text length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"输入不能为空！" message:nil delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil, nil];
        [alert show];
    }else
    {
    //判断添加类型
    if ([self.str isEqualToString:@"addType"]) {
        //单例的使用
        DataBaseHandler *dataHandler = [DataBaseHandler shareInstance];
        
        //打开数据库(调用)
        [dataHandler openDB];
        
//        //创建表成功
//        [dataHandler createTable];
//        
        //添加数据
        Payout *payout = [[Payout alloc] init];
        payout.type = self.textFieldStr.text;
        
        [dataHandler insertTypePayout:payout];
    }
    //判断添加成员
    else if ([self.str isEqualToString:@"addPeople"])
    {
        //单例的使用
        DataBaseHandler *dataHandler = [DataBaseHandler shareInstance];
        
        //打开数据库(调用)
        [dataHandler openDB];
        
//        //创建表成功
//        [dataHandler createPersonnelTable];
        
        //添加数据
        Payout *payout = [[Payout alloc] init];
        payout.personnel = self.textFieldStr.text;
        
        [dataHandler insertPersonnelPayout:payout];

    }
}

   
}




//释放内存
- (void)dealloc
{
    [_str release];
    [_textFieldName release];
    [_textFieldStr release];
    [super dealloc];
}




@end
